package com.skyworth.web.admin.menu.service.impl;

import com.google.gson.JsonObject;
import com.skyworth.web.admin.menu.domain.MenuCategoryModel;
import com.skyworth.web.admin.menu.form.MenuCategoryForm;
import com.skyworth.web.admin.menu.repository.MenuCategoryRepository;
import com.skyworth.web.admin.menu.service.MenuCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by skyworth on 2016/8/8.
 */
@Service
public class MenuCategoryServiceImpl implements MenuCategoryService {

	@Autowired
	private MenuCategoryRepository menuCategoryRepository;

	@Override
	public void addMenuCategory(MenuCategoryForm menuCategoryForm) {
		MenuCategoryModel menuCategoryModel = new MenuCategoryModel();
		menuCategoryModel.categoryId = menuCategoryForm.getCategoryId();
		menuCategoryModel.menuId = menuCategoryForm.getMenuId();
		menuCategoryRepository.save(menuCategoryModel);
	}

	@Override
	public void delMenuCategory(long id) {
		menuCategoryRepository.delete(id);
	}

	@Override
	public MenuCategoryModel updateMenuCategory(MenuCategoryForm menuCategoryForm) {
		MenuCategoryModel menuCategoryModel = this.findMenuCategoryModel(menuCategoryForm.id);
		menuCategoryModel.categoryId = menuCategoryForm.getCategoryId();
		menuCategoryModel.menuId = menuCategoryForm.getMenuId();
		menuCategoryRepository.save(menuCategoryModel);
		return menuCategoryModel;
	}

	@Override
	public MenuCategoryModel findMenuCategoryModel(long id) {
		return menuCategoryRepository.findOne(id);
	}

	@Override
	public List<MenuCategoryModel> listMenuCategory(JsonObject params) {
		long store_id = 0;
		if(null != params){
			store_id = params.get("store_id").getAsLong();
		}
		return menuCategoryRepository.findAllGroupByCategoryId(store_id);
//		return Lists.newArrayList(menuCategoryRepository.findAll());
	}
}
